import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class ProductHtmlDescView extends StatefulWidget {
  final String? htmlContent;

  const ProductHtmlDescView({
    Key? key,
    required this.htmlContent,
  }) : super(key: key);

  @override
  _ProductHtmlDescViewState createState() => _ProductHtmlDescViewState();
}

class _ProductHtmlDescViewState extends State<ProductHtmlDescView> {
  bool isExpanded = false;
 double defaultHeight = 0.0;  // 初始高度为366
  double expandedHeight = 700.0;  // 展开后更大高度

  // 清理不必要的 HTML 标签
  String cleanHtml(String? html) {
    if (html == null) return '';
    String cleanedHtml = html.replaceAll(RegExp(r'<span[^>]*font-family:Wingdings[^>]*>'), '');
  
    cleanedHtml = cleanedHtml.replaceAll(RegExp(r'<span[^>]*font-family: "Times New Roman"[^>]*>'), '');
    cleanedHtml = cleanedHtml.replaceAll(RegExp(r'class="MsoListParagraph"[^>]*'), '');
    cleanedHtml = cleanedHtml.replaceAll(RegExp(r'font-variant-[^>]*'), '');  // 移除字体变体等样式
    return cleanedHtml;
  }

  @override
  Widget build(BuildContext context) {
    String? cleanedHtml = cleanHtml(widget.htmlContent);
    return AnimatedContainer(
      duration: Duration(milliseconds: 300),
      height: cleanedHtml.isNotEmpty ? 340 : 0, 
      margin: EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(15),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // HTML 内容部分
          Expanded(
            child: SingleChildScrollView(
              // physics: isExpanded ? null : NeverScrollableScrollPhysics(),
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 0, vertical: 0),
                child: Html(
                  data: cleanedHtml,
                  style: {
                    "html": Style(
                      fontSize: FontSize(15),
                      fontFamily: "sans-serif",
                      padding: HtmlPaddings.only(top: 10, left: 10, right: 10, bottom: 0),
                    ),
                    "p": Style(
                      margin: Margins.zero,
                      padding: HtmlPaddings.zero,
                    ),
                    ".MsoListParagraph": Style(
                      fontFamily: "sans-serif",
                      fontSize: FontSize(13),
                    ),
                    "p.MsoListParagraph": Style(
                      margin: Margins.zero,
                      padding: HtmlPaddings.zero,
                      color: Colors.transparent,
                      textAlign: TextAlign.justify,
                    ),
                  },
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
